package cn.tedu.shoppingmall.controller;

import cn.tedu.shoppingmall.mapper.ReportClsMapper;
import cn.tedu.shoppingmall.mapper.ReportMapper;
import cn.tedu.shoppingmall.vo.Commodity;
import cn.tedu.shoppingmall.vo.Report;
import cn.tedu.shoppingmall.vo.ReportCls;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cglib.core.Local;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Api(tags = "7资讯模块")
@RestController
public class AdminReportController {
    @Autowired(required = false)
    ReportMapper reportMapper;
    @Autowired(required = false)
    ReportClsMapper reportClsMapper;
    @Value("${dirPath}")
    private String dirPath;

    //查询类的所有信息
    @RequestMapping("/admin/selectToReportCls")
    public List<ReportCls> selectToReportCls(){
        return reportClsMapper.select();
    }

    //根据类查询该类所有资讯信息
    @RequestMapping("/admin/selectToReport")
    public List<Report> selectToReport(Integer clsid){
        return reportMapper.selectByClsid(clsid);
    }
    //查询所有资讯信息
    @RequestMapping("/admin/selectToReportAll")
    public List<Report> selectToReport(){
        List<Report> reports = reportMapper.select();
//        for (Report r : reports){
//            //将时间格式变一下,方便页面展示
//         SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//            String dateString = sdf.format(r.getCreated());
//            r.setCreatedStr(dateString);
//        }

        return reports;
    }

    //通过id查询单个咨询信息
    @RequestMapping("/admin/ReportSelectById")
    public Report ReportSelectById(int id){
        return reportMapper.SelectById(id);
    }


    //更新资讯
    @RequestMapping("/admin/reportUpdate")
    //                                实体类          用户id        图片文件
    public void commodityupdate(Report report, Integer id, MultipartFile picFile){
        System.out.println("report = " + report + ", id = " + id + ", picFile = " + picFile);
        //如果未填写,则默认原来不变
        //先根据用户id查询原有数据
        Report reportOld = reportMapper.SelectById(id);
        System.out.println("reportold = " + reportOld);
        if (report.getTitle()==null || "".equals(report.getTitle())){
            report.setTitle(reportOld.getTitle());
        }if (report.getContent()==null || "".equals(report.getContent())){
            report.setContent(reportOld.getContent());
        }if (report.getClsId()==null || report.getClsId()==0){
            report.setClsId(reportOld.getClsId());
        }if (report.getUrl()==null || "".equals(report.getUrl())){
            report.setUrl(reportOld.getUrl());
        }
        System.out.println("report1 = " + report + ", id = " + id + ", picFile = " + picFile);

        if (!picFile.isEmpty()) {
            String fileName = picFile.getOriginalFilename(); //获得文件原始名称xxxx.jpg
            String suffix = fileName.substring(fileName.lastIndexOf(".")); //结果  ".jpg"
            fileName = UUID.randomUUID() + suffix; //  UUID.jpg
            //文件夹路径
            SimpleDateFormat f = new SimpleDateFormat("/yyyy/MM/dd/");
            //new Date()当前系统时间  /2021/12/20/
            String datePath = f.format(new Date());  //2021-12-27 20:25:53  ----> /2021/12/20/
            File dirFile = new File(dirPath + datePath);
            if (!dirFile.exists()) {
                dirFile.mkdirs();
            }
            //文件完整路径      根目录   时间目录  文件目录    d:/zjxl/2021/12/20/UUID.jpg  /static/index.html
            String filePath = dirPath + datePath + fileName;
            try {
                //保存文件
                picFile.transferTo(new File(filePath));
            } catch (IOException e) {
                System.out.println("URL装配失败，请检查路径");
            }
            report.setUrl(datePath + fileName);
        }
        report.setCreated(LocalDateTime.now());
        report.setId(id);  //根据id查
        System.out.println("report2 = " + report + ", id = " + id + ", picFile = " + picFile);
        reportMapper.update(report);
    }


    //添加资讯
    @RequestMapping("/admin/reportInsert")
    public void reportInsert(Report report, MultipartFile picFile){
        System.out.println("report = " + report + ", picFile = " + picFile);
        if (!picFile.isEmpty()) {
            String fileName = picFile.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            fileName = UUID.randomUUID() + suffix;
            //文件夹路径
            SimpleDateFormat f = new SimpleDateFormat("/yyyy/MM/dd/");
            //new Date()当前系统时间  /2021/12/20/
            String datePath = f.format(new Date());
            File dirFile = new File(dirPath + datePath);
            if (!dirFile.exists()) {
                dirFile.mkdirs();
            }
            //文件完整路径
            String filePath = dirPath + datePath + fileName;
            try {
                //保存文件
                picFile.transferTo(new File(filePath));
            } catch (IOException e) {
                System.out.println("URL装配失败，请检查路径");
            }
            report.setUrl(datePath + fileName);//把包含日期路径的图片路径封装
        }
        report.setCreated(LocalDateTime.now());
        reportMapper.insert(report);
    }

    //删除资讯
    @RequestMapping("/admin/ReportDelete")
    public void ReportDeleteDelete(int id){
        reportMapper.delete(id);
    }


}
